package adata;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.ApplicationModel;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import db.DbException;
import forms.ApplicantForm;
import forms.Degree;
import forms.FileUploadForm;

public class DegreeDisciplinePageAction extends Action{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
		    HttpServletRequest request, HttpServletResponse response) 
		    throws DbException, Exception{
		
		Degree ad = (Degree) form;

		String currentDegreeTitle = request.getParameter("degreeTitle");
		String currentMonth = request.getParameter("month");
		String currentYear = request.getParameter("year");
		String currentGPA = request.getParameter("GPA");
		
		
		///////////////////////////////////////////////////////////////////////////////
		
		
		
		List<Degree> theDegreeArray;
		if (request.getSession().getAttribute("TheSessionDegreeArray") != null)
		{
			// retrieve list from session attribute if it exists
			theDegreeArray = (LinkedList<Degree>) request.getSession().getAttribute("TheSessionDegreeArray");
		}
		else
		{
			// create the list otherwise
			theDegreeArray = new LinkedList<Degree>();
		}
			
		Integer currentCount = (Integer)(request.getSession().getAttribute("SessionCounter"));
		
		if (request.getParameter("major").equals("other"))
		{
			String otherMajor = request.getParameter("otherMajor");
			theDegreeArray.get(currentCount).setMajor(otherMajor);
			request.getSession().setAttribute("otherMajor", otherMajor);
		}
		else
		{
			String major = request.getParameter("major");
			theDegreeArray.get(currentCount).setMajor(major);
		}
		
		theDegreeArray.get(currentCount).setDegreeTitle (currentDegreeTitle);
		theDegreeArray.get(currentCount).setMonth (currentMonth);
		theDegreeArray.get(currentCount).setYear (currentYear);
		theDegreeArray.get(currentCount).setGPA (currentGPA);
		
				
		
		Degree myForm = (Degree) form;

		// Process the FormFile
		FormFile myFile = myForm.getMyFile();
		String contentType = myFile.getContentType();
		String fileName = myFile.getFileName();
		int fileSize = myFile.getFileSize();
		byte[] fileData = myFile.getFileData();
		request.setAttribute("filename", fileName);
		request.setAttribute("filesize", fileSize);
		List<String> words = new ArrayList<String>();
		words.add("hello");
		words.add("world");
		request.setAttribute("words", words);
		
		
		theDegreeArray.get(currentCount).setMyFile(myFile);
		
		
		request.getSession().setAttribute ("TheSessionDegreeArray", theDegreeArray);
		
		
		return mapping.findForward("success");
		
	}
}
